﻿using UnityEngine;
using System.Collections;
using NCmd;
using System;

public class Npc : Creature
{
    public new NpcTableData TableData
    {
        get
        {
            return base.TableData as NpcTableData;
        }
    }

    private NpcAI _ai;

    public override void Init(AddSceneCreature serverData, CreatureTableData tableData)
    {
        base.Init(serverData, tableData);

        var npcTableData = tableData as NpcTableData;

        if (!string.IsNullOrEmpty(npcTableData.AIScript))
        {
            _ai = typeof(NpcAI).Assembly.CreateInstance(npcTableData.AIScript) as NpcAI;
            _ai.Init(this);
            _ai.Pause = true;
        }
    }

    // 是否可被攻击
    public override bool CanBeAttack(Creature attacker)
    {
        if (!base.CanBeAttack(attacker))
        {
            return false;
        }

        return TableData.Type == (int)NpcType.Monster;
    }

    protected override void onRespawn()
    {
        base.onRespawn();

        if (_ai != null)
        { _ai.Pause = false; }
    }

    protected override void onDie()
    {
        if (_ai != null)
        { _ai.Pause = true; }

        base.onDie();
    }

    protected override void Update()
    {
        base.Update();

        if (_ai != null)
        {
            _ai.Loop();
        }
    }
}
